package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;

import logger.LogLevel;
import logger.SimpleLogger;
import util.Config;
import util.Debug;

/**
 * gekapselter Action-Listener
 * @author Bernd Taube
 * @version 1.0
 *
 */
public class MyActionListener implements ActionListener, ItemListener {

	// Instanz-Variablen
	private Controller control;
	private Actions action;
	
	/**
	 * Konstruktor
	 * @param control Controller über den der ActionListener abgewickelt wird
	 */
	public MyActionListener(Controller control) {
		this.control = control;
	}

	/**
	 * Getter der Action
	 * @return
	 */
	public Actions getAction() {
		return this.action;
	}

	/**
	 * Setter der Action
	 * @param action
	 */
	public void setAction(Actions action) {
		this.action = action;
	}

	/**
	 * und los!
	 * Action wird an den Controller zurückgegeben
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Debug.print("EVENT: " + e.getActionCommand());
		control.actionPerformed(action);
	}
	
	/**
	 * itemStatus wurde gewechselt: itemArgument wird gesetzt CityModel,
	 * CountryModel...
	 */
	@Override
	public void itemStateChanged(ItemEvent e) {
		
		if ((control.itemArgument = e.getItem()) == null && e.getStateChange() != ItemEvent.SELECTED) {
				if (Config.DEBUG){
					SimpleLogger.log(LogLevel.DEBUG,"get item from combobox failed");
				}
				try {
					throw new ControllerException("selected item is unitialized");
				} catch (ControllerException e1) {
					e1.printStackTrace();
				}
			
		}
	}

}
